﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using Relocation.Base;
using Relocation.Base.UI;
using Relocation.Com;
using Relocation.Data;

namespace Relocation
{
	public partial class ProjectWindow : BaseForm
	{
		public Projects Project;

		public ProjectWindow():base()
		{
			Initialize();
		}

		public ProjectWindow(Session s):base(s)
		{
			Initialize();
		}

		//传递参数Projects对象和Session对象修改项目
		public ProjectWindow(Session session, Projects project)
			: this(session)
		{
			this.Project = project;
			this.SetValueToInput(this.Project);
		}

		private void Initialize()
		{
			InitializeComponent();
			this.InitializeField();
			ToolTip toolTip = new ToolTip();
			toolTip.SetToolTip(displayBT, "查看许可证");
			
		}

		/// <summary>
		/// 初始化字段
		/// </summary>
		private void InitializeField()
		{
			this.projectSave.Tag = new ControlTag(new RoleInfo(Session.KEY_ROLE_ADMIN, Session.KEY_ROLE_OPERATOR));
			this.Field_ProjectCode.Tag = new ControlTag(Projects.GetPropName(t => t.projectCode), new ValidatorInfo(new ValidatorInfo.ValidatorHandler(delegate(Control control) {
				control.Text = control.Text.Trim();
				string value = control.Text;
				if (string.IsNullOrEmpty(value))
				{
					return new ValidatorInfo.ValidatorResult(false, "必须填写");
				} else
				{
					bool isExists = (this.Project == null) ?
						(this.Session.DataModel.Projects.Any(t => control.Text.Equals(t.projectCode)))
						:
						(this.Session.DataModel.Projects.Any(t => control.Text.Equals(t.projectCode) && (!t.projectID.Equals(this.Project.projectID))));
					return new ValidatorInfo.ValidatorResult(!isExists, isExists ? "项目编号重复" : string.Empty);
				}
			})));
			this.Field_ProjectName.Tag = new ControlTag(Projects.GetPropName(t => t.projectName), new ValidatorInfo(true));

			Field_Issue.Tag = new ControlTag(Projects.GetPropName(t => t.issueDate));
			Field_Codes.Tag = new ControlTag(Projects.GetPropName(t => t.codes));
			Field_Relocate_name.Tag = new ControlTag(Projects.GetPropName(t => t.relocate_name));
			Field_Relocate_person.Tag = new ControlTag(Projects.GetPropName(t => t.relocate_person));
			Field_Relocate_phone.Tag = new ControlTag(Projects.GetPropName(t => t.relocate_phone));
			Field_Department_name.Tag = new ControlTag(Projects.GetPropName(t => t.department_name));
			Field_Department_person.Tag = new ControlTag(Projects.GetPropName(t => t.department_person));
			Field_Department_phone.Tag = new ControlTag(Projects.GetPropName(t => t.department_phone));
			Field_FilePath.Tag = new ControlTag(Projects.GetPropName(t => t.filePath));
			Field_Remark.Tag = new ControlTag(Projects.GetPropName(t => t.remark));
		}

		protected void SetValueToInput(Projects project)
		{
			base.SetValueToInput(project);
			if (project.filePath != null && project.filePath != "")
			{
				this.displayBT.Visible = true;
				this.scanBT.Visible = false;
				this.deleteBT.Visible = true;
			}
			this.FieldIsChanged = false;			
		}



		//修改和新增保存
		private void projectSave_Click(object sender, EventArgs e)
		{
			try
			{
				bool isNew = this.Project == null;
				if (isNew)   //新增
				{
					this.Project = new Projects();
					Project.created = DateTime.Now;
				}
				this.GetValueFromInput(this.Project);
				this.Project.updated = System.DateTime.Now;
				Session.DataModel.Save(Project);
				this.FieldIsChanged = false;
				this.DialogResult = DialogResult.OK;
				this.Close();
			}
			catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
				MyMessagebox.Error("操作失败！");
			}
		}

		//扫描附件
		private void scanBT_Click(object sender, EventArgs e)
		{
			try
			{
				FileWindow fileWin = new FileWindow(Session);
				if (fileWin.ShowDialog() == DialogResult.OK)
				{
					this.Field_FilePath.Text = fileWin.strPath;
					this.displayBT.Visible = true;
					this.scanBT.Visible = false;
					this.deleteBT.Visible = true;
				}
			}
			catch (Exception ex)
			{
				Log.Error(ex.Message);
				MyMessagebox.Show("没有连接上扫描仪！");
			}
		}

		//删除附件
		private void deleteBT_Click(object sender, EventArgs e)
		{
			try
			{
				if (MyMessagebox.Confirm("是否删除此许可证文件？") == DialogResult.Yes)
				{
					if (System.IO.File.Exists(Field_FilePath.Text.Trim()))
						System.IO.File.Delete(this.Field_FilePath.Text.Trim());
					this.Field_FilePath.Text = "";
					this.scanBT.Visible = true;
					this.deleteBT.Visible = false;
					this.displayBT.Visible = false;
				}
			}
			catch (Exception ex)
			{
				Log.Error(ex.Message);
				MyMessagebox.Error(ex.Message);
			}
		}

		//显示附件
		private void displayBT_Click(object sender, EventArgs e)
		{
			try
			{
				if (Field_FilePath.Text.Trim() != "")
				{
					if (System.IO.File.Exists(Field_FilePath.Text.Trim())) Process.Start(Field_FilePath.Text.Trim());
					else MyMessagebox.Show("未找到许可证文件，有可能被删除或转移！");
				}
			}
			catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
				MyMessagebox.Error("操作失败！");

			}
		}	

	}
}
